package com.yc.forum.servlet;

import com.yc.forum.util.Utils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "SavePostImgServlet", value = "/SavePostImgServlet")
@MultipartConfig(
        maxFileSize = 1024*1024+10,
        maxRequestSize = 1024*1024*30,
        fileSizeThreshold = 0,//小：内存，大：硬盘
        location = ""
)
public class SavePostImgServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("================================/SavePostImgServlet----->doPost------========================");
        response.setContentType("text/html;charset=utf-8");
        Part file=request.getPart("file");
        System.out.println("file大小============="+file.getSize());
        //地址
        String path;
        String filename=request.getParameter("uploadImgUrlName");
        System.out.println("filename====="+filename);
        if (file.getSize() != 0) {
            //获得ServletContext对象
            ServletContext servletContext = this.getServletContext();
            //保存路径
            path = servletContext.getRealPath("cover_img")+"/";
            System.out.println(path);
            path=path+filename;
            System.out.println(path);
            file.write((path));
        }else {
            path=null;
        }
        Utils.toJson("发布成功",response);
    }
}
